$(function() {
    var cols=6;
	var rows_delete=0;
	var mode="add";
	var m_id=0;
	var class_id=0;
    var url='controls/student_model.php';
 
	var objSelectTable=[];
	
	initialData();
	
	 function initialData(){
	 
		//BlockUI();
		
		$.get(url, {"m":"class"},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboClass_Search").append(
						$('<option></option>').val(text).html(index)
					);
				});
				$("#cboClass_Search").val(1);
				$("#cboClass_Search").change();
			}
		});
		 $("#cboClass_Search").append($('<option></option>').val(0).html(''));
		 
		 $.get(url, {"m":"status"},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboStatus_Search").append(
						$('<option></option>').val(text).html(index)
					);
				});
				
			}
		});
		 $("#cboStatus_Search").append($('<option></option>').val(0).html(''));
		 
		LoadData('','',1,1,0);
	}
	
	$("#cboClass_Search").change(function(){
		var cid=$(this).val();
		
		$("#cboRoom_Search").find('option').remove();	
		
		$.get(url, {"m":"room","i":cid},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboRoom_Search").append(
						$('<option></option>').val(text).html(index)
					);
				});
			}
		});
	});	
	
	 function LoadData(co,name,cid,r,status){
		BlockUI();
        $( "#resultData tbody tr" ).remove();

        $('#resultData').dataTable( {
			"bProcessing": true,
			"bFilter": false,
			"bPaginate": true,
			"bLengthChange": false,
			"sAjaxSource": url,
			"oLanguage": {
				"sEmptyTable":     "ไม่พบข้อมูล"
			},
			"bDestroy": true,
			"sServerMethod": "POST",
			"fnServerParams": function ( aoData ) {
					aoData.push( { "name": "m", "value": "load_old" },
								{"name":"cid","value":cid},
								{"name":"r","value":r},
								{"name":"co","value":co},
								{"name":"n","value":name},
								{"name":"s","value":status}
							);
				},
			"aoColumnDefs":[
				{  "bVisible": false , "aTargets": [ 1 ] },	
				{   "bSortable": true, "aTargets": [ 2 ] ,"sClass":"left"},	
				{   "bSortable": true, "aTargets": [ 3 ] ,"sClass":"left" },	
				{   "bSortable": true, "aTargets": [ 4 ] ,"sClass":"left" },
				{   "bSortable": true, "aTargets": [ 5 ] ,"sClass":"center" },	
				{   "bSortable": true, "aTargets": [ 6 ] ,"sClass":"center" },	
				{   "bSortable": true, "aTargets": [ 7 ] ,"sClass":"center" },	
				{   "bSortable": true, "aTargets": [ 8 ] ,"sClass":"center" },	
	  
				{ "aTargets": [ 9 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						if (_it==1)
							return "<a href='#'><img src='img/edit-file.png' class='btnEdit' style='cursor:pointer;'/></a>";
						else 
							return "";
					}
				},
				{"aTargets": [ 10 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						if (_te==1)
							return "<a href='#popupConfirm' data-toggle='modal'><img src='img/delete-file.png' class='btnDel' style='cursor:pointer;'/></a>";
						else 
							return "";
					}
				}
			],"fnDrawCallback": function ( oSettings ) {
				 var that = this;
				/* Need to redo the counters if filtered or sorted */
                 var iTotalRecords = oSettings.fnRecordsTotal();
 
					if (iTotalRecords > 0){ 	
						if ( oSettings.bSorted || oSettings.bFiltered )
						{
							for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
							{
								$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
							}
						}
					}
					$.unblockUI();
				} 
		 } );
		
      }
	$(".btnEdit").live('click',function(){
		
		var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
		var data =$('#resultData').dataTable().fnGetData(rows);
		m_id = data[1];	
		$("#my_t").val('o');
		$("#my").val(m_id);
		 
		$("#myForm").submit();
	});
 //Popup confirm delete
	$(".btnDel").live('click',function(){
		var rows = $(this).closest("tr").get(0);
		rows_delete = $('#resultData').dataTable().fnGetPosition(rows);

	}); 
	
	$("#btnDelete").live('click', function(){
		var row_id =$('#resultData').dataTable().fnGetData(rows_delete);
		
		var id=row_id[1];
		 
		 $.ajax({
				 type: 'POST',
				 cache: false,
				 url: url,
				 data: {"m":"del_student","id":id},
				 success: function(data){
					$("#popupConfirm").modal('hide');
					$('#resultData').dataTable().fnDeleteRow( rows_delete );
					$('#resultData').dataTable().fnDraw();
				 }
			 });
 
	}); 
	
	$("#btnSearch").click(function(){
		var _code = $("#txtCode_Search").val();
		var _name = $("#txtStudent_Search").val();
		var _cid = $("#cboClass_Search").val();
		var _room = $("#cboRoom_Search").val();
		var _status = $("#cboStatus_Search").val();
		LoadData(_code , _name, _cid, _room,_status );
	});
  
	 
	$(".btnAbort").live('click',function(){
		 var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
		 var data =$('#resultData').dataTable().fnGetData(rows);
		 m_id = data[1];
		  alert(m_id);
	});
  
	$(".btnMove").live('click',function(){
		var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
		var data =$('#resultData').dataTable().fnGetData(rows);
		m_id = data[1];
		 
		$.get(url, {"m":"room","i":data[3]},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboRoom_to").append(
						$('<option></option>').val(text).html(index)
					);
				});
			}
		});
		
	});
	
	$("#btnSave2").live('click',function(){
		$("#txtReasonRoom").jqBootstrapValidation(
			{preventSubmit: false,
				submitSuccess: function ($form, event) {
					event.preventDefault();
				}
			}); 
		var _reason = $.trim($("#txtReasonRoom").val());	
		var _room=$("#cboRoom_to").val();
		dataSet={"m":"move","id":m_id,"r":_reason ,"room":_room};  
		 
		if (_reason!="" ){
			$.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: dataSet,
				success: function(data){
					$("#popup2").modal('hide');
					LoadData('','',0,0,0);
				}
			});
			
		}
		
		$("#txtReasonRoom").jqBootstrapValidation("destroy");
	});
	
	$("#btnSave").live('click',function(){
		$("#txtReason").jqBootstrapValidation(
			{preventSubmit: false,
				submitSuccess: function ($form, event) {
					event.preventDefault();
				}
			}); 
	 
		var _reason = $.trim($("#txtReason").val());
		 
		var dataSet;

		dataSet={"m":"exit","id":m_id,"r":_reason };  
		 
		if (_reason!="" ){
			$.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: dataSet,
				success: function(data){
					$("#popup").modal('hide');
					LoadData('','',0,0,0);
				}
			});
			
		}
		 $("#txtReason").jqBootstrapValidation("destroy");
	});
	
	$("#btnPrintDraft").click(function(){
	  $("#resultData").printThis();
		return false;
           
	});	
	
    
});


